package com.orange.common.util

import android.os.Parcelable
import android.text.TextUtils
import com.blankj.utilcode.util.GsonUtils
import com.tencent.mmkv.MMKV
import java.lang.reflect.Type
import java.util.Collections

object MMKVUtils {

    val mmkv: MMKV = MMKV.defaultMMKV()

    fun put(key: String, value: Any?): Boolean {
        return when (value) {
            is String -> mmkv.encode(key, value)
            is Float -> mmkv.encode(key, value)
            is Boolean -> mmkv.encode(key, value)
            is Int -> mmkv.encode(key, value)
            is Long -> mmkv.encode(key, value)
            is Double -> mmkv.encode(key, value)
            is ByteArray -> mmkv.encode(key, value)
            else -> putAny(key, value)
        }
    }

    /**
     * 这里使用安卓自带的Parcelable序列化，它比java支持的Serializer序列化性能好些
     */
    fun <T : Parcelable> put(key: String, t: T?): Boolean {
        if (t == null) {
            return false
        }
        return mmkv.encode(key, t)
    }

    fun put(key: String, sets: Set<String>?): Boolean {
        if (sets == null) {
            return false
        }
        return mmkv.encode(key, sets)
    }

    /**
     * 保存对象 转json 保存
     */
    fun putAny(key: String, value: Any?): Boolean {
        return put(key,GsonUtils.toJson(value))
    }

    /**
     * 解析json 转对象
     */
    inline fun <reified T : Any> getAny(key: String): T? {
        return getString(key).let {
            if (it.isNotBlank()){
                GsonUtils.fromJson(it,T::class.java)
            }else{
                null
            }
        }
    }

    fun getInt(key: String,default: Int = 0): Int {
        return mmkv.decodeInt(key, default)
    }

    fun getDouble(key: String,default: Double = 0.00): Double {
        return mmkv.decodeDouble(key, default)
    }

    fun getLong(key: String,default: Long = 0L): Long {
        return mmkv.decodeLong(key, default)
    }

    fun getBoolean(key: String,default:Boolean = false): Boolean {
        return mmkv.decodeBool(key, default)
    }

    fun getFloat(key: String,default: Float = 0F): Float {
        return mmkv.decodeFloat(key, default)
    }

    fun getByteArray(key: String): ByteArray? {
        return mmkv.decodeBytes(key)
    }

    fun getString(key: String,default: String = ""): String {
        return mmkv.decodeString(key, default) ?: ""
    }

    /**
     * SpUtils.getParcelable<Class>("")
     */
    inline fun <reified T : Parcelable> getParcelable(key: String): T? {
        return mmkv.decodeParcelable(key, T::class.java)
    }

    fun getStringSet(key: String): Set<String>? {
        return mmkv.decodeStringSet(key, Collections.emptySet())
    }

    fun removeKey(key: String) {
        mmkv.removeValueForKey(key)
    }

    fun clearAll() {
        mmkv.clearAll()
    }

    /** 供java 使用 */
    @JvmStatic
    fun saveObject(key: String, any: Any?) {
        if (any == null) {
            put(key, "")
        } else {
            put(key, GsonUtils.toJson(any))
        }
    }
    @JvmStatic
    fun <T> getObject(key: String, type: Type): T? {
        val json = getString(key)
        return if (TextUtils.isEmpty(json)) {
            null
        } else {
            GsonUtils.fromJson(json, type)
        }
    }
}